<template>
  <div class="goods-contain">
    <div class="search">
      <el-input
        v-model="vagueKey"
        placeholder="本位码/商品名称/条码/包装规格"
        style="width: 235px"
      />
      <el-button style="margin-left: 10px; color: white; background-color: dodgerblue" @click="getTableData">搜索</el-button>
      <!--        <el-button disabled style="color: white; background-color: dodgerblue">同步本位码</el-button>-->
      <el-button style="color: white; background-color: dodgerblue" @click="exportFile">导出</el-button>
    </div>
    <div class="table">
      <el-table
        tooltip-effect="dark"
        style="width: 100%;"
        height="calc(100vh - 330px)"
        :data="list"
      >
        <el-table-column label="序号" type="index" />
        <el-table-column label="本位码" prop="medicineCodeNumber" />
        <el-table-column label="包装规格" prop="packSpec" />
        <el-table-column label="批准文号" prop="medicineOfficeNumber" />
        <el-table-column label="商品名称" prop="medicineGeneralName" />
        <el-table-column label="剂型" prop="doseType" />
        <el-table-column label="规格" prop="specifications" />
        <el-table-column label="生产厂家" prop="productionEprice" />
        <el-table-column label="计量单位" prop="measureUnit" />
        <!--          <el-table-column label="更新日期" prop="produceDate"></el-table-column>-->
        <!--          <el-table-column label="更新人" prop="valDate"></el-table-column>-->
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        :current-page.sync="currentPage1"
        layout="total,prev, pager, next, sizes,jumper"
        :total="totalRecord"
        :page-sizes="[10, 20, 30, 40, 50, 100]"
        :page-size="10"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
import { getShengYjGoods, exportShengYjGoods } from '@/api/match'
export default {
  name: 'Goods',
  data() {
    return {
      list: [],
      pageNo: 1,
      pageSize: 10,
      vagueKey: '',
      totalRecord: 0,
      loading: false,
      currentPage1: 1
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    handleCurrentChange(val) {
      this.pageNo = val
      this.getTableData()
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getTableData()
    },
    getTableData() {
      const params = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        vagueKey: this.vagueKey
      }
      getShengYjGoods({ ...params }).then(res => {
        this.list = res.data.results
        this.pageNo = res.data.page
        this.pageSize = res.data.pageSize
        this.totalPage = res.data.totalPage
        this.totalRecord = res.data.totalRecord
      })
        .finally(_ => {
          this.loading = false
        })
    },
    exportFile() {
      const params = {
        pageSize: this.pageSize,
        pageNo: this.pageNo,
        vagueKey: this.vagueKey
      }
      exportShengYjGoods({ ...params }).then(res => {
        console.log(res)
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel;charset=utf-8' })
        const patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
        const reg = /^["](.*)["]$/g
        const contentDisposition = res.headers['content-disposition']
        const result = patt.exec(contentDisposition)
        const filename = result[1]
        const downloadElement = document.createElement('a')
        const href = window.URL.createObjectURL(blob)
        downloadElement.download = decodeURI(filename.replace(reg, '$1'))
        downloadElement.style.display = 'none'
        downloadElement.href = href
        document.body.appendChild(downloadElement)
        downloadElement.click()
        document.body.removeChild(downloadElement)
        window.URL.revokeObjectURL(href)
      }).finally(_ => {
        this.loading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  @import '~@/style/drugAdmin/dataMatch/match.scss';
</style>
